﻿''' -----------------------------------------------------------------------------
''' Project	 : IPCommon
''' Class	 : Core.IPCommon.CIPConvert
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Lớp này có nhiệm Convert các kiểu dữ liệu
''' theo chuẩn của IP.
''' </summary>
''' <remarks>
''' Các trường hợp hay dùng là:
''' <list >
''' 
''' </list>
''' </remarks>
''' <history>
''' 	[csung]	2/22/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
''' 
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Public Class CIPConvert
    'Private Shared m_obj_culture As New System.Globalization.CultureInfo("vi-VN")
    'Private Shared m_obj_culture As New System.Globalization.CultureInfo("en_US")
    Private Shared m_obj_culture As System.Globalization.CultureInfo = System.Globalization.CultureInfo.CurrentCulture

#Region "Convert from Boolean to YN and reversed"
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Convert từ Boolean sang xâu YN
    ''' </summary>
    ''' <param name="i_bBoolean">Boolean</param>
    ''' <returns>String</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ToYNString(ByVal i_bBoolean As Boolean) As String
        If i_bBoolean Then
            Return "Y"
        Else
            Return "N"
        End If
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Convert từ Boolean sang xâu YN
    ''' </summary>
    ''' <param name="i_bBoolean">Object</param>
    ''' <returns>String</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ToYNString(ByVal i_obj_Boolean As Object) As String
        Debug.Assert(TypeOf (i_obj_Boolean) Is Boolean, "Doi tuong truyen vao ham ToYNString phai co kieu Boolean")
        If CType(i_obj_Boolean, Boolean) Then
            Return "Y"
        Else
            Return "N"
        End If
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Convert từ xâu YN sang Boolean
    ''' </summary>
    ''' <param name="i_str_YN">String</param>
    ''' <param name="v_hash_table"></param>
    ''' <returns>Boolean</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ToBoolean(ByVal i_str_YN As String, Optional ByVal v_hash_table As Hashtable = Nothing) As Boolean
        If v_hash_table Is Nothing Then
            Select Case i_str_YN
                Case "Y"
                    Return True
                Case "N"
                    Return False
                Case Else
                    Debug.Assert(False, "Xau truyen vao ham ToBoolean cua IPConvert khong hop le - tuanqt")
            End Select
        Else
            Select Case i_str_YN
                Case CType(v_hash_table(True), String)
                    Return True
                Case CType(v_hash_table(False), String)
                    Return False
                Case Else
                    Debug.Assert(False, "Xau truyen vao ham ToBoolean cua IPConvert khong hop le - tuanqt")
            End Select
        End If
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Convert từ xâu YN sang Boolean
    ''' </summary>
    ''' <param name="i_obj_YN">Object</param>
    ''' <returns>Boolean</returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ToBoolean(ByVal i_obj_YN As Object) As Boolean
        Debug.Assert(Not i_obj_YN Is Nothing, "doi tuong truyen vao CIPConvert.ToBoolean phai khac nothing - tuanqt")
        Debug.Assert(TypeOf (i_obj_YN) Is String, "Doi tuong truyen vao CIPConvert.ToBoolean phai co kieu string - tuanqt")
        Dim v_str_temp = CType(i_obj_YN, String)
        Select Case v_str_temp
            Case "Y"
                Return True
            Case "N"
                Return False
            Case Else
                Debug.Assert(False, "Xau truyen vao ham ToBoolean cua IPConvert khong hop le - tuanqt")
        End Select
    End Function
#End Region

#Region "Convert to String from another type"
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Convert một số thành một xâu trong Query
    ''' </summary>
    ''' <param name="i_dc_Input">Decimal</param>
    ''' <returns>String</returns>
    ''' <remarks>
    ''' Cái này chỉ dùng khi xây dựng câu lệnh Select.
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ToStrInQuery(ByVal i_dc_Input As Decimal) As String
        Dim v_objFormat As New System.Globalization.NumberFormatInfo
        v_objFormat.NumberDecimalSeparator = "."
        Return System.Convert.ToString(i_dc_Input, v_objFormat)
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Convert một số thành chuỗi theo định dạng của nước hiện tại
    ''' </summary>
    ''' <param name="i_dc_Input"></param>
    ''' <returns>String
    ''' </returns>
    ''' <example>
    ''' <list>
    ''' <item> 1000 => "1.000" nếu định dạng chung là  việt nam</item>
    ''' <item> 1000 => "1,000" nếu định dạng chung là US</item>
    ''' </list>
    ''' </example>
    ''' <remarks> 
    ''' </remarks> 
    ''' <history>
    ''' 	[csung]	2/22/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function ToStr(ByVal i_dc_Input As Decimal) As String
        Return Convert.ToString(i_dc_Input, m_obj_culture.NumberFormat)
    End Function

    Public Shared Function ToStr(ByVal i_dc_input As Decimal, ByVal i_str_format As String) As String
        Return Convert.ToString(i_dc_input, m_obj_culture.NumberFormat).Format(m_obj_culture.NumberFormat, "{0:" & i_str_format & "}", i_dc_input)
    End Function

    Public Shared Function ToStr(ByVal i_dat_Input As DateTime) As String
        Return Convert.ToString(i_dat_Input, m_obj_culture.DateTimeFormat).Format("{0:dd/MM/yyyy}", i_dat_Input)
    End Function

    Public Shared Function ToStr(ByVal i_dat_Input As DateTime, ByVal i_str_format As String) As String
        Return Convert.ToString(i_dat_Input, m_obj_culture.DateTimeFormat).Format("{0:" & i_str_format & "}", i_dat_Input)
    End Function

    Public Shared Function ToStr(ByVal i_obj_Input As Object) As String
        Debug.Assert(Not i_obj_Input Is Nothing, "Tham so truyen vao ham ToStr phai khac nothing")
        Select Case i_obj_Input.GetType.ToString()
            Case "System.Decimal"
                Return ToStr(CType(i_obj_Input, Decimal))
            Case "System.DateTime"
                Return ToStr(CType(i_obj_Input, DateTime))
            Case Else
                Return CType(i_obj_Input, String)
        End Select
    End Function

    Public Shared Function ToStr(ByVal i_obj_Input As Object, ByVal i_str_format As String) As String
        Debug.Assert(Not i_obj_Input Is Nothing, "Tham so truyen vao ham ToStr phai khac nothing")
        Debug.Assert(Not i_obj_Input Is Nothing, "Tham so truyen vao ham ToStr phai khac nothing")
        Select Case i_obj_Input.GetType.ToString()
            Case "System.Decimal"
                Return ToStr(CType(i_obj_Input, Decimal), i_str_format)
            Case "System.DateTime"
                Return ToStr(CType(i_obj_Input, DateTime), i_str_format)
            Case Else
                Return CType(i_obj_Input, String)
        End Select
    End Function

    ' chuy?n sang sâu ngày vn
    Public Shared Function Date2Str_VNFormat(ByVal i_date As DateTime) As String
        Return CDateTime.Date2Str_VNFormat(i_date)
    End Function
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Convert 1 đoạn văn thành 1 giá trị datetime sử dụng kiểu VN: dd/mm/yyyy
    ''' </summary>
    ''' <param name="i_str"></param>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/21/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Shared Function Str2Date(ByVal i_str As String) As DateTime
        Return CDateTime.Str2Date(i_str)
    End Function
#End Region

#Region "Convert to Decimal from another type"
    Public Shared Function ToDecimal(ByVal i_obj_input As Object) As Decimal
        Debug.Assert(Not i_obj_input Is Nothing, "Doi tuong truyen vao ham ToDecimal phai khac nothing - tuanqt")
        Return Convert.ToDecimal(i_obj_input, m_obj_culture.NumberFormat)
    End Function
    Public Shared Function is_valid_number(ByVal i_obj_input As Object) As Boolean
        Try
            If (i_obj_input Is Nothing) Then Return False
            ToDecimal(i_obj_input)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Public Shared Function get_default_dec_format() As String
        Return "#,###"
    End Function
#End Region

#Region "Convert to Datetime from another type"

    Public Shared Function ToDatetime(ByVal i_str_date As String) As DateTime
        Dim v_myDateTime As System.DateTime
        'Neu khong truyen vao format thi format theo kieu IPCore
        v_myDateTime = CDateTime.Str2Date(i_str_date)
        Return v_myDateTime
    End Function

    Public Shared Function ToDatetime(ByVal i_str_date As String, ByVal i_str_format As String) As DateTime
        Dim v_myDateTime As System.DateTime
        'Neu truyen vao format thi lay format 
        Dim v_strFormat As String = m_obj_culture.DateTimeFormat.ShortDatePattern
        v_myDateTime = System.DateTime.ParseExact(i_str_date, _
                    i_str_format, _
                    m_obj_culture)
        Return v_myDateTime
    End Function

    Public Shared Function ToDatetime(ByVal i_obj_date As Object) As DateTime
        Debug.Assert(Not i_obj_date Is Nothing, "Doi tuong truyen vao ham ToDecimal phai khac nothing - tuanqt")
        Debug.Assert(TypeOf (i_obj_date) Is String, "Doi tuong truyen vao ham ToDate phai co kieu string - tuanqt")
        Dim v_str_temp_date As String = CType(i_obj_date, String)
        Return ToDatetime(v_str_temp_date)
    End Function

    Public Shared Function ToDatetime(ByVal i_obj_date As Object, ByVal i_str_format As String) As DateTime
        Debug.Assert(Not i_obj_date Is Nothing, "Doi tuong truyen vao ham ToDecimal phai khac nothing - tuanqt")
        Debug.Assert(TypeOf (i_obj_date) Is String, "Doi tuong truyen vao ham ToDate phai co kieu string - tuanqt")
        Dim v_str_temp_date As String = CType(i_obj_date, String)
        Return ToDatetime(v_str_temp_date, i_str_format)
    End Function

    Public Shared Function is_valid_datetime(ByVal i_str_input As String) As Boolean
        Try
            ToDatetime(i_str_input)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function is_valid_datetime(ByVal i_obj_input As Object) As Boolean
        Try
            ToDatetime(i_obj_input)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function is_valid_datetime(ByVal i_obj_input As Object, ByVal i_str_format As String) As Boolean
        Try
            ToDatetime(i_obj_input, i_str_format)
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function Date_fromUs_toDisplayStr(ByVal i_date As DateTime) As String
        If i_date = IPConstants.c_DefaultDate Then
            Return String.Empty
        Else
            Return ToStr(i_date)
        End If
    End Function

    Public Shared Function get_default_date_format() As String
        Return "dd/MM/yyyy"
    End Function

#End Region
    Public Shared Function ConvertToJpeg(ByVal fileName As String) As Image
        Dim retVal As Image = Nothing
        Using fs As FileStream = File.OpenRead(fileName)
            retVal = ConvertToJpeg(fs)
            fs.Close()
        End Using

        Return retVal
    End Function

    Public Shared Function ConvertToJpeg(ByVal imgStream As Stream) As Image
        Dim retVal As Image = Nothing
        Dim retStream As Stream = New MemoryStream()

        Using img As Image = Image.FromStream(imgStream, True, True)
            img.Save(retStream, ImageFormat.Jpeg)
            retStream.Flush()
        End Using
        retVal = Image.FromStream(retStream, True, True)

        Return retVal
    End Function


End Class
